﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace KD.Sms.Card
{
    public class MfMain
    {
        /// <summary>
        /// 本类的唯一实例
        /// </summary>
        private static MfMain OWNER = null;

        /// <summary>
        /// 获取类的唯一实例
        /// </summary>
        /// <returns></returns>
        public static MfMain Instance
        {
            get
            {
                if (MfMain.OWNER == null)
                {
                    MfMain.OWNER = new MfMain();
                }
                return MfMain.OWNER;
            }
        }

        private Thread _thrMain;
        private bool _bRun = false;
        private short _mfCom { set; get; }
        private int _mfBaudRate { set; get; }
        /// <summary>
        /// 启动读卡
        /// </summary>
        public bool DoStartRead(string com, int baudrate)
        {
            try
            {
                _mfCom = short.Parse(com.ToUpper().Replace("COM", ""));
                _mfBaudRate = baudrate;
                _bRun = true;
                _thrMain = new Thread(new ThreadStart(DoRead)) { IsBackground = true };
                _thrMain.Start();
                return true;
            }
            catch (Exception ex)
            {
                ActionCenter.Action.Instance.DoSetCarNum(DiscriminateType.CardRead, "启动明华读卡器失败!详见日志!", true);
                Log.LogHelper.DoWriteLogTypeClass(Log.LogType.Program, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "启动明华读卡器失败!!");
                Log.LogHelper.DoWriteLogTypeClass(Log.LogType.Program, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, ex.ToString());
                return false;
            }
        }

        /// <summary>
        /// 停止读卡
        /// </summary>
        public void DoStopRead()
        {
            try
            {
                _bRun = false;
                System.Threading.Thread.Sleep(500);
                if (_thrMain != null)
                    _thrMain.Abort();
            }
            catch (Exception ex)
            {
                ActionCenter.Action.Instance.DoSetCarNum(DiscriminateType.CardRead, "关闭明华读卡器失败!详见日志!", true);
                Log.LogHelper.DoWriteLogTypeClass(Log.LogType.Program, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "关闭明华读卡器失败!!");
                Log.LogHelper.DoWriteLogTypeClass(Log.LogType.Program, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, ex.ToString());
            }
        }

        /// <summary>
        /// 读卡线程
        /// </summary>
        private void DoRead()
        {
            string _strCarNum = string.Empty;
            while (_bRun)
            {
                MfReader obj = null;
                try
                {
                    _strCarNum = string.Empty;
                    obj = new MfReader(_mfCom, _mfBaudRate);
                    if (obj.Open())
                    {
                        _strCarNum = obj.GetCardNo();
                        if (!string.IsNullOrEmpty(_strCarNum))
                        {
                            ActionCenter.Action.Instance.DoSetCarNum(DiscriminateType.CardRead, _strCarNum, string.IsNullOrEmpty(_strCarNum));
                            //第一次刷卡成功后等待五秒再读
                            System.Threading.Thread.Sleep(10000);
                        }
                        if (!obj.Close())
                            ActionCenter.Action.Instance.DoSetCarNum(DiscriminateType.CardRead, string.Format("关闭读卡器失败[{0}][{1}]", _mfCom, _mfBaudRate), true);
                    }
                    else
                        ActionCenter.Action.Instance.DoSetCarNum(DiscriminateType.CardRead, string.Format("打开读卡器失败[{0}][{1}]", _mfCom, _mfBaudRate), true);
                }
                catch (Exception ex)
                {
                    ActionCenter.Action.Instance.DoSetCarNum(DiscriminateType.CardRead, string.Format("读卡出现未知异常!详见日志![{0}][{1}]", _mfCom, _mfBaudRate), true);
                    Log.LogHelper.DoWriteLogTypeClass(Log.LogType.Program, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "读卡出现未知异常!!");
                    Log.LogHelper.DoWriteLogTypeClass(Log.LogType.Program, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, ex.ToString());
                }
                finally
                {
                    if (obj != null)
                    {
                        if (obj.IsOpen)
                            obj.Close();
                        obj = null;
                    }
                }
                System.Threading.Thread.Sleep(200);
            }
        }








    }
}
